package com.ljx.funchat.chat.service.adapter;

import com.ljx.funchat.chat.domain.entity.Room;
import com.ljx.funchat.chat.domain.entity.RoomFriend;
import com.ljx.funchat.chat.domain.entity.RoomGroup;
import com.ljx.funchat.chat.domain.entity.RoomGroupMember;
import com.ljx.funchat.chat.domain.enums.RoomStatusEnum;
import com.ljx.funchat.chat.domain.vo.resp.GroupMemberPageResp;
import com.ljx.funchat.user.domain.entity.User;
import com.ljx.funchat.user.domain.enums.RoomMemberRoleEnum;
import com.ljx.funchat.user.domain.vo.resp.UserFriendSearchResp;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class RoomAdapter {
    public static Room buildRoomSave(Integer type) {
        Room room = new Room();
        room.setType(type);
        return room;
    }

    public static RoomFriend buildRoomFriendSave(List<Long> ids, Long roomId) {
        List<Long> resIds = ids.stream().sorted().collect(Collectors.toList());
        RoomFriend roomFriend = new RoomFriend();
        roomFriend.setRoomId(roomId);
        roomFriend.setUid1(resIds.get(0));
        roomFriend.setUid2(resIds.get(1));
        roomFriend.setRoomKey(resIds.get(0) + "_" + resIds.get(1));
        roomFriend.setStatus(RoomStatusEnum.NORMAL.getStatus());
        return roomFriend;
    }

    public static List<GroupMemberPageResp> buildRoomMemberPagResp(Map<Long, User> userMap, List<RoomGroupMember> list) {
        return list.stream().map(item -> {
            GroupMemberPageResp groupMemberPageResp = new GroupMemberPageResp();
            groupMemberPageResp.setUid(item.getUid());
            groupMemberPageResp.setRoleId(item.getRole());
            groupMemberPageResp.setActiveStatus(userMap.get(item.getUid()).getActiveStatus());
            return groupMemberPageResp;
        }).collect(Collectors.toList());
    }

    public static RoomGroupMember buildRoomMemberFirstSave(Long uid) {
        RoomGroupMember roomGroupMember = new RoomGroupMember();
        roomGroupMember.setGroupId(1L); // 默认全员群
        roomGroupMember.setUid(uid);
        roomGroupMember.setRole(RoomMemberRoleEnum.MEMBER.getRole());
        return roomGroupMember;
    }

    public static UserFriendSearchResp.GroupInfo buildSearch(RoomGroup roomGroup, boolean roomFlag) {
        UserFriendSearchResp.GroupInfo groupInfo = new UserFriendSearchResp.GroupInfo();
        groupInfo.setId(roomGroup.getId());
        groupInfo.setName(roomGroup.getName());
        groupInfo.setAvatar(roomGroup.getAvatar());
        groupInfo.setJoin(roomFlag);
        return groupInfo;
    }
}
